<?php

set_time_limit(0);
ini_set('memory_limit', '1500M');
Yii::import('application.mlm.core.mlm_function');
Yii::import('application.mlm.core.binary.bonus.bonus_gen_node_products');
Yii::import('application.mlm.core.mlm_lib_gi');
Yii::import('application.mlm.core.binary.*');
Yii::import('application.mlm.core.matrix.*');
Yii::import('application.mlm.core.board.*');

class JoinController extends MemberController {

    public function actionStep1() {

        $type = function_lib::uri_segment(4);
        $sponsor = function_lib::uri_segment(5);
        $upline = function_lib::uri_segment(6);
        $position = function_lib::uri_segment(7);

        $hash = base64_encode($position . "#" . $upline . "#" . $sponsor);
        Yii::app()->controller->redirect(array('/member/join/' . $type . '/' . $hash));
    }

    public function actionNew() {

        $show = TRUE;
        $message = "";
        $model = new registrasi_form('registrasi');

        $hash = function_lib::uri_segment(4);
        $query_string = base64_decode($hash);
        $arr_data = explode("#", $query_string);
        $model->member_sponsor = $data['sponsor_mid'] = $arr_data[2];
        $model->member_upline = $data['upline_mid'] = $arr_data[1];
        $model->position = $data['position'] = ($arr_data[0] == 'kiri') ? 'L' : 'R';

        if (isset($_POST['registrasi_form'])) {
            //set attributnya
            $model->setAttributes($_POST['registrasi_form'], false);
            if ($model->validate()) {

                //ambil data yang ada di attribut
                //network nya
                $arr_network['sponsor_mid'] = $model->member_sponsor;
                $arr_network['upline_mid'] = $model->member_upline;
                $arr_network['position'] = $model->position;
                //serial nya
                $arr_card['serial_id'] = $model->serial_id;
                $arr_card['serial_mid'] = '';
                $arr_card['serial_pin'] = $model->serial_pin;
                //data membernya
                //$arr_member['register_id'] = $model['register_id'];
                $arr_member['password'] = md5($model->password);
                $arr_member['member_nickname'] = $model->member_nickname;
                $arr_member['member_name'] = $model->member_name;
                $arr_member['member_email'] = $model->member_email;
                $arr_member['member_phone'] = $model->member_phone;
                $arr_member['member_mobilephone'] = $model->member_mobilephone;
                
                  /*
                 * BANK 
                 */
                $arr_member['member_bank_account_name'] = $model->member_name;
                $arr_member['member_bank_id'] = $model->member_bank_id;
                $arr_member['member_bank_city'] = $model->member_bank_city;
                $arr_member['member_bank_branch'] = $model->member_bank_branch;
                $arr_member['member_bank_account_no'] = $model->member_bank_account_no;

                //saatnya registrasi
                $mlm = new mlm_lib_gi();
                //data array buat bonus distributor
                $bonus_arr = array('sponsor', 'sponsor_upgrade', 'match', 'gen_node', 'gen_node_extra', 'gen_node_products', 'gen_match','extra_sponsor','royalti_angsuran');
                $mlm->set_bonus($bonus_arr)
                        ->set_date(date("Y-m-d"))
                        ->set_hu(7);
                $mlm->set_data_serial($arr_card)
                        ->set_data_member($arr_member)
                        ->set_data_network($arr_network);
                $member_network_id = $mlm->get_network_id_arr();
                $network_mid = function_lib::get_network_mid($member_network_id[1]);
                $network_id = $member_network_id[1];
                $sponsor = new mlm_sponsor;
                $sponsor->set_date(date("Y-m-d"))->gen_node(function_lib::get_network_id($model->member_sponsor));

                //sekarang buat bonus pasangan
                $match = new mlm_match;
                $match->set_date(date("Y-m-d"));
                $match->execute();
                //buat jaringan matrixnya
                $mlm_matrix = new mlm_matrix;
                $mlm_matrix->set_date(date("Y-m-d"))->set_height(10)->set_width(4)->set_network_id($network_id)->set_network_mid($network_mid)->insert_node();

                $message = $mlm->get_message();
                $show = FALSE;
            }
        }
        $this->render('new', array('model' => $model, 'show' => $show, 'message' => $message));
    }

    public function actionClone() {

        $show = TRUE;
        $message = "";
        $model = new registrasi_form('registrasi');
        $member = sys_member::model()->findByPk(Yii::app()->user->getId());
        $password = sys_password::model()->find('password_network_id=:network_id', array(':network_id' => Yii::app()->user->getId()));

        $hash = function_lib::uri_segment(4);
        $query_string = base64_decode($hash);
        $arr_data = explode("#", $query_string);
        $model->member_sponsor = $data['sponsor_mid'] = $arr_data[2];
        $model->member_upline = $data['upline_mid'] = $arr_data[1];
        $model->position = $data['position'] = ($arr_data[0] == 'kiri') ? 'L' : 'R';

        $model->password = $password->password_value;
        $model->password2 = $password->password_value;
        $model->member_name = $member->member_name;
        $model->member_nickname = $member->member_nickname;
        $model->member_email = $member->member_email;
        $model->member_sex = $member->member_sex;
        $model->member_address = $member->member_address;
        $model->member_city_id = $member->member_city_id;
        $model->member_state_id = $member->member_state_id;
        $model->member_zipcode = $member->member_zipcode;
        $model->member_birth_place = $member->member_birth_place;
        $model->member_birth_date = $member->member_birth_date;
        $model->member_identity_type = $member->member_identity_type;
        $model->member_identity_no = $member->member_identity_no;
        $model->member_phone = $member->member_phone;
        $model->member_mobilephone = $member->member_mobilephone;
        $model->member_devisor_name = $member->member_devisor_name;
        $model->member_devisor_relationship = $member->member_devisor_relationship;
        $model->member_devisor_phone = $member->member_devisor_phone;
        $model->member_bank_id = $member->member_bank_id;
        $model->member_bank_city = $member->member_bank_city;
        $model->member_bank_branch = $member->member_bank_branch;
        $model->member_bank_account_name = $member->member_bank_account_name;
        $model->member_bank_account_no = $member->member_bank_account_no;

        if (isset($_POST['registrasi_form'])) {
            //set attributnya
            $model->setAttributes($_POST['registrasi_form'], false);
            if ($model->validate()) {
                
                //ambil data yang ada di attribut
                //network nya
                $arr_network['sponsor_mid'] = $model->member_sponsor;
                $arr_network['upline_mid'] = $model->member_upline;
                $arr_network['position'] = $model->position;
                //serial nya
                $arr_card['serial_id'] = $model->serial_id;
                $arr_card['serial_mid'] = '';
                $arr_card['serial_pin'] = $model->serial_pin;
                //data membernya
                //$arr_member['register_id'] = $model['register_id'];
                $arr_member['password'] = $model->password;
                $arr_member['member_name'] = $model->member_name;
                $arr_member['member_nickname'] = $model->member_nickname;
                $arr_member['member_email'] = $model->member_email;
                $arr_member['member_sex'] = $model->member_sex;
                $arr_member['member_address'] = $member->member_address;
                $arr_member['member_city_id'] = $member->member_city_id;
                $arr_member['member_state_id'] = $member->member_state_id;
                $arr_member['member_zipcode'] = $member->member_zipcode;
                $arr_member['member_birth_place'] = $member->member_birth_place;
                $arr_member['member_birth_date'] = $member->member_birth_date;
                $arr_member['member_identity_type'] = $member->member_identity_type;
                $arr_member['member_identity_no'] = $member->member_identity_no;
                $arr_member['member_phone'] = $member->member_phone;
                $arr_member['member_mobilephone'] = $model->member_mobilephone;
                $arr_member['member_devisor_name'] = $member->member_devisor_name;
                $arr_member['member_devisor_relationship'] = $member->member_devisor_relationship;
                $arr_member['member_devisor_phone'] = $member->member_devisor_phone;
                $arr_member['member_bank_id'] = $member->member_bank_id;
                $arr_member['member_bank_city'] = $member->member_bank_city;
                $arr_member['member_bank_branch'] = $member->member_bank_branch;
                $arr_member['member_bank_account_name'] = $member->member_bank_account_name;
                $arr_member['member_bank_account_no'] = $member->member_bank_account_no;

                //saatnya registrasi
                $mlm = new mlm_lib_gi();
                //data array buat bonus distributor
                $bonus_arr = array('sponsor', 'sponsor_upgrade', 'match', 'gen_node', 'gen_node_extra', 'gen_node_products', 'gen_match','extra_sponsor','royalti_angsuran');
                $mlm->set_bonus($bonus_arr)
                        ->set_date(date("Y-m-d"))
                        ->set_hu(7);
                $mlm->set_data_serial($arr_card)
                        ->set_data_member_full($arr_member)
                        ->set_data_network($arr_network);
                $member_network_id = $mlm->get_network_id_arr();
                $network_mid = function_lib::get_network_mid($member_network_id[1]);
                $network_id = $member_network_id[1];
                $sponsor = new mlm_sponsor;
                $sponsor->set_date(date("Y-m-d"))->gen_node(function_lib::get_network_id($model->member_sponsor));

                //sekarang buat bonus pasangan
                $match = new mlm_match;
                $match->set_date(date("Y-m-d"));
                $match->execute();
                //buat jaringan matrixnya
                $mlm_matrix = new mlm_matrix;
                $mlm_matrix->set_date(date("Y-m-d"))->set_height(10)->set_width(4)->set_network_id($network_id)->set_network_mid($network_mid)->insert_node();

                $message = $mlm->get_message();
                $show = FALSE;
            }
        }
        $this->render('clone', array('model' => $model, 'show' => $show, 'message' => $message));
    }

    public function actionGet_area_name() {
        $area_id = $_POST['area_id'];
        $array_area = Yii::app()->db->createCommand()->from("ref_area")->where("area_par_id = '{$area_id}'")->queryAll();
        $data = CHtml::listData($array_area, 'area_id', 'area_name');
        foreach ($data as $area_id => $area_name) {
            echo CHtml::tag('option', array('value' => $area_id), CHtml::encode($area_name), true);
        }
    }

    public function actionajax_request() {

        $member = sys_network::model()->with('rel_network_to_member')->find(array('select' => '*',
            'condition' => 'network_mid=:network_mid',
            'params' => array(':network_mid' => $_POST['ajax_request']),
                ));
        //var_dump($member);
        if (empty($member)) {
            echo "member tidak ditemukan";
        } else {
            $member_name = $member->rel_network_to_member;
            echo $member_name->member_name . "<br />" . function_lib::get_area_name($member_name->member_city_id);
        }
    }

}
